#ifndef __RawFileUtils_H__
#define __RawFileUtils_H__


#include <string>
#include <unordered_map>

#include <ace/xcomponent/native_interface_xcomponent.h>
#include <napi/native_api.h>
#include <uv.h>

#include "../common/native_common.h"

#include <rawfile/raw_dir.h>
#include <rawfile/raw_file.h>
#include <rawfile/raw_file_manager.h>

class RawFileUtils
{
public:
    static bool InitResourceManager(napi_env env, napi_value info);
    
    static napi_value nativeResourceManagerInit(napi_env env, napi_callback_info info) {
        size_t      argc = 1;
        napi_value  args[1];
        NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr));
        InitResourceManager(env, args[0]);
        return nullptr;
    }
    
    static RawFileUtils& GetInstance() {
        static RawFileUtils instance;
        return instance;
    }

    /**
     * 判断是否在rawfile目录下
     * @param filename
     * @return
     */
    bool fileIsExistInRawPath(const char *filename);
    bool fileIsExistInWriteablePath(const char *filename);
    /**
     * 获取文件在可写目录的全路径
     * @param filename
     * @return
     */
    const char *getFilePathInWriteablePath(const char *filename);
    /**
     * 复制rawfile文件到缓存目录
     * @param filename
     * @return
     */
    const char *copyRawFile2WriteablePath(const char *filename);

    bool fileIsExistInSpecifyPath(const char *filename, const char *specifyPath);

    const char *copyRawFile2SpecifyPath(const char *filename, const char *specifyPath);

    RawFile64 *Open(const char *fileName) { return OH_ResourceManager_OpenRawFile64(nativeResourceManager_, fileName); }
    void Close(RawFile64 *file) {
        if (file == nullptr) {
            return;
        }
        OH_ResourceManager_CloseRawFile64(file);
    }
    long GetSize(RawFile64 *file) { return OH_ResourceManager_GetRawFileSize64(file); }
    long Read(RawFile64 *file, void *buf, size_t length) { return OH_ResourceManager_ReadRawFile64(file, buf, length); }
    int GetDirSize(RawDir *rawDir) { return OH_ResourceManager_GetRawFileCount(rawDir); }

private:    

    std::vector<std::string> searchFiles(const char *folder, bool recursive = false);
    std::string getWritableFilePath();
    bool createDirectory(const std::string &path);
    std::string copyRawFile2SpecifyPath(RawFile64 *rawFile, std::string &path, std::string fileName);
    static NativeResourceManager *nativeResourceManager_;
};



#endif // __RawFileUtils_H__
